<?php
/**
 * Created by PhpStorm.
 * User: trisatria
 * Date: 1/6/14
 * Time: 1:17 PM
 */
namespace Admin\Controller;
use Admin\Model\AdminCommend;
use Admin\Model\AdminCommendTable;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Velacolib\Utility\Utility;

class CommendController extends AbstractActionController
{
    private $commendTable;
    public function onDispatch(\Zend\Mvc\MvcEvent $e)
    {
        $this->service_locator_str = 'Admin\Model\AdminCommendTable';
        $this->sm = $this->getServiceLocator();
        $this->commendTable = $this->sm->get($this->service_locator_str);
       $user =  Utility::CheckLogin($this);
        if ($user->user_type != 3 || $user->user_type ==2) {
            $this->redirect()->toUrl('/admin/login');
        }
        return parent::onDispatch($e);
    }

    public function init(){
        //$this->Acl->SetModule('Project');
        //if(!$this->Acl->Check())
          //  $this->redirect()->toRoute('home', array ('action' => 'index'));
    }

    public function indexAction()
    {

    }

    public function addAction(){
       if($this->getRequest()->isPost()){
           $data = $this->params()->fromPost();
           $data['create_date'] = date("Y-m-d H:i:s");
           $Admin_model = new AdminCommend();
           $Admin_model->exchangeArray($data);
           $this->commendTable->saveCommend($Admin_model);

          $this->redirect()->toRoute('admin/child', array(
              'controller' => 'topic',
              'action' =>  'detail',
              'id' =>$data['topic_id'],
          ));
       }
    }
    public function deleteAction(){
        $id = $this->params()->fromRoute('id');
        if($id){
            $this->commendTable->deleteCommend($id);
            $topic_id = $this->params()->fromRoute('topic_id');
            $this->redirect()->toRoute('admin/child', array(
                'controller' => 'topic',
                'action' =>  'detail',
                'id' =>$topic_id,
            ));

        }
        else{

        }
    }
    public function editAction(){
        echo 'edit';
    }
}